<template>
  <div class="cloud-teller-manage" v-loading="loading" element-loading-text="加载中。。。">
    <div>
      <el-card class='cloud-teller-card'>
        <el-form ref='queryForm' :model='queryForm' size='mini' label-position='top'>
          <el-row :gutter="10">

            <el-col :md='6' :lg='4'>
              <el-form-item label='渠道名称' prop='CHLCODE'>
                <el-select v-model='queryForm.CHLCODE' filterable>
                  <el-option v-for='item in CHLNAMEOpions' :key='item.CHLCODE' :label='item.CHLNAME' :value='item.CHLCODE'>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :md='6' :lg='4'>
              <el-form-item label='交易名称' prop='FLOWID'>
                <el-select v-model='queryForm.FLOWID' filterable>
                  <el-option v-for='item in FLOWNAMEOpions' :key='item.FLOWID' :label='item.FLOWNAME' :value='item.FLOWID'>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :md="3" :lg="2" class="searchBtnMarginTop">
              <el-button type='primary' size='mini' icon='el-icon-search' @click="queryModel('search')">查询</el-button>
            </el-col>
            <el-col :md="3" :lg="2" class="searchBtnMarginTop">
              <el-button type='warning' size='mini' icon='el-icon-refresh' @click="clearForm('queryForm')">重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <el-card style="margin-top: 10px">
        <el-col :lg="2" :md="2" v-if="btnPermission('Button_Warning_Add')">
          <el-button type="primary" size="mini" @click="handleClickDialog('add')" icon='el-icon-plus'>新增</el-button>
        </el-col>
        <el-col :lg="2" :md="2" v-if="btnPermission('Button_Warning_Edit')">
          <el-button type="primary" size="mini" @click="handleClickDialog('update')" icon='el-icon-edit' :disabled="!updateButtonShow">修改</el-button>
        </el-col>
        <el-col :lg="2" :md="2" v-if="btnPermission('Button_Warning_Delete')">
          <el-button type="danger" size="mini" @click="deleteConfirm" icon='el-icon-delete' :disabled="!delButtonShow">删除</el-button>
        </el-col>
        <el-table ref="tableData" :data='tableDataPage' border min-height="400" width='100%' class="tableMarginTop" header-row-class-name="tableHeaderClass"
          highlight-current-row @selection-change="selectChange">
          <el-table-column type="selection" width="40px"></el-table-column>
          <el-table-column prop='CHLCODE' label='渠道码'></el-table-column>
          <el-table-column prop='CHLNAME' label='渠道名称'></el-table-column>
          <el-table-column prop='FLOWID' label='交易码'></el-table-column>
          <el-table-column prop='FLOWNAME' label='交易名称'></el-table-column>
          <el-table-column prop='MAINTAINDATE' label='维护日期'></el-table-column>
          <el-table-column prop='REMARK' label='备注'></el-table-column>
        </el-table>
        <el-pagination style="text-align:right;margin-top:20px;" v-show="this.tableData" :current-page="currentPage" background :page-size="pageSize"
          @current-change="changePage" layout="total, prev, pager, next, jumper" :total="tableData.length">
        </el-pagination>
      </el-card>

      <keep-alive>
        <component :is='dialogList.CloudTeller' :option='addDialogOptions' @refreshTable="queryModel('search')"></component>
      </keep-alive>
       <keep-alive>
        <component :is='dialogList.CloudTellerUpd' :option='updDialogOptions' @refreshTable="queryModel('search')"></component>
      </keep-alive>
    </div>

  </div>
</template>

<script>
  import CloudTeller from "./dialog/channelTran";
  import CloudTellerUpd from "./dialog/channelTranUpd";
  import selectTree from "@/components/selectTree2/";
  import toolBox from "@/CTManage/utils/toolBox";
  import getGlobalParams from '@/common/utils/getGlobalParams';
  //通讯相关
  import request from "@/common/utils/requestCT";
  import channelTransaction from "@/message/CTManage/channel-transaction/channel-transaction-query";
  import channelTransactionDel from "@/message/CTManage/channel-transaction/channel-transaction-del";
  
  import channelNameQuery from "@/message/shared/channel-name-query";
  import transactionNameQuery from "@/message/shared/transaction-name-query";
  export default {
    name: "cloud-teller-manage",
    components: {
      selectTree
    },
    data() {
      return {
        //页面公用
        loading: false,
        //表格
        tableTotal: 0,
        tableData: [],
        currentPage: 1,
        pageSize: getGlobalParams.get("PageSize"),
        delButtonShow: false,
        updateButtonShow: false,
        queryForm: {
          CHLCODE: '',
          FLOWID: ''
        },
        currentRow: [],
        telleridSelection: [],
        FLOWNAMEOpions: [],
        CHLNAMEOpions: [],
        dialogList: {
          CloudTeller,
          CloudTellerUpd
        },
        updDialogOptions: {
          isShow: false,
          font: '',
          isUpdate: true,
          currentRow: [],
          CHLNAMEOpions: [],
          FLOWNAMEOpions: []
        },
        addDialogOptions:{
           isShow: false,
          font: '',
          isUpdate: true,
          currentRow: [],
          CHLNAMEOpions: [],
          FLOWNAMEOpions: []
        },

      };
    },
    mounted() {
      this.queryModel();
      this.channelNameQuery();
      this.transactionNameQuery();
    },
    computed: {
      tableDataPage() {
        return this.tableData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
      }
    },
    methods: {
      // 获取渠道名称方法
      async channelNameQuery() {
        this.loading = true;
        this.tableData = [];
        this.tableTotal = 0;
        let resBody = new channelNameQuery();
        let response = await request(resBody);
        this.loading = false;
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.CHLNAMEOpions = response.RSP_BODY.resultSet;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          });
        }
      },
      // 获取交易名称方法
      async transactionNameQuery() {
        this.loading = true;
        this.tableData = [];
        this.tableTotal = 0;
        let resBody = new transactionNameQuery();
        let response = await request(resBody);

        this.loading = false;
        if (response.SYS_HEAD.ReturnCode == "000000") {
          console.log(response.RSP_BODY.resultSet)
          this.FLOWNAMEOpions = response.RSP_BODY.resultSet;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          });
        }
      },

      // 表格改变选择方法
      selectChange(val) {
        this.currentRow = [];
        this.telleridSelection = [];
        if (val.length == 1) {
          this.delButtonShow = true;
          this.updateButtonShow = true;
          this.currentRow = val
        } else if (val.length > 1) {
          this.delButtonShow = false;
          this.updateButtonShow = false;
        } else {
          this.delButtonShow = false;
          this.updateButtonShow = false;
        }
        for (var i = 0; i < val.length; i++) {
          this.telleridSelection[i] = val[i];
        }
        // console.log(this.telleridSelection)
      },

      // 点击新增修改按钮事件
      handleClickDialog(val) {
        // console.log(this.currentRow)
        if (val === 'add') {
          this.addDialogOptions.currentRow = [];
          this.addDialogOptions.CHLNAMEOpions = this.CHLNAMEOpions;
          this.addDialogOptions.FLOWNAMEOpions = this.FLOWNAMEOpions;
          this.addDialogOptions.font = '新增';
          this.addDialogOptions.isUpdate = false;
          this.addDialogOptions.isShow = !this.addDialogOptions.isShow;
        } else {
          this.updDialogOptions.currentRow = [];
          this.updDialogOptions.CHLNAMEOpions = this.CHLNAMEOpions;
          this.updDialogOptions.FLOWNAMEOpions = this.FLOWNAMEOpions;
          this.updDialogOptions.font = '修改';
          this.updDialogOptions.isUpdate = true;
          this.updDialogOptions.currentRow = this.currentRow;
          this.updDialogOptions.isShow = !this.updDialogOptions.isShow;
        } 
      },

      async delCloudTeller() {
        this.loading = true;
        let resBody = new channelTransactionDel();
        resBody.data.FLOWID = this.telleridSelection[0].FLOWID;
        resBody.data.CHLCODE = this.telleridSelection[0].CHLCODE;
        let response = await request(resBody);
        this.loading = false;
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.queryModel('search');
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          });
        }
      },
      deleteConfirm() {
        this.$confirm("此操作将永久删除" + this.telleridSelection[0].CHLCODE + " 是否继续？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            this.delCloudTeller();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },

      changePage(val) {
        this.currentPage = val
      },
      clearForm(item) {
        this.$refs["" + item].resetFields();
      },
      async queryModel(data) {
        if(data == 'search'){
          this.currentPage = 1;
        }
        this.loading = true;
        this.tableData = [];
        this.tableTotal = 0;
        let resBody = new channelTransaction();
        resBody.data.FLOWID = this.queryForm.FLOWID;
        resBody.data.CHLCODE = this.queryForm.CHLCODE;
        
        resBody.QueryPageNo = this.currentPage;
        resBody.QueryRowNum = this.pageSize;
        let response = await request(resBody);

        this.loading = false;
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.tableData = response.RSP_BODY.resultSet;
          this.tableTotal = response.RSP_BODY.total * 1 
          // || response.QUERY_INFO_HEAD.QueryAllDataCount;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          });
        }
      },


      // handleSizeChange() {
      //   this.queryModel();
      // },
    },

    async created() {}
  };

</script>


<style lang="scss" scoped>
  .el-date-editor.el-input {
    width: 100% !important;
  }

  .el-card {
    overflow: inherit;
  }

</style>
